package com.sunnada.edu.system.org.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sunnada.edu.system.org.dao.OrgDao;



@Service
@Transactional
public class OrgServiceImpl implements OrgService {
	@Resource
	OrgDao orgDao;

	@Override
	public List<?> orgTree(HashMap<String, String> params) {
		return orgDao.orgTree(params);
	}

	@Override
	public int getOrgCots(HashMap<String, String> params) {
		return orgDao.getOrgCots(params);
	}

	@Override
	public List<?> findOrgs(HashMap<String, String> params) {
		return orgDao.findOrgs(params);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public String orgAdd(HashMap<String, String> params) {
		String orgLName = params.get("orgName"); 
		//Map org = orgDao.getOrgByOrgcord(params);//获取父节点
		String msg = "";
		String bigCode = orgDao.orgTreebig(params); // 获取父组织编码下的最大子组织编码
		if ("0".equals(bigCode)) { // 等于0表示该父组织下没有子组织
			bigCode = params.get("parentCode") + "10";
		} else {
			bigCode = String.valueOf(Integer.valueOf(bigCode).intValue() + 1);
		}
		params.put("orgCode", bigCode); // 内部组织编码
		
		params.put("recId", UUID.randomUUID().toString());
		int num = orgDao.orgAdd(params); // 往组织表添加
		if (num == 0) {
			msg = "添加失败！";
		}
		return msg;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Map getOrgByRecId(HashMap<String, String> params) {
		return orgDao.getOrgByRecId(params);
	}

	@Override
	public String orgUpdate(HashMap<String, String> params) {
		String msg = "";
		int num = orgDao.orgUpdate(params);
		if (num == 0) {
			msg = "修改失败！";
		}
		return msg;
	}

	@Override
	public String orgDel(HashMap<String, String> params) {
		String msg = "";
		int num = orgDao.orgDel(params);
		if (num == 0) {
			msg = "删除失败！";
		}
		return msg;
	}

	@Override
	public String orgEnable(HashMap<String, String> params) {
		String msg = "";
		int num = orgDao.orgEnable(params);
		if (num == 0) {
			msg = "组织启用失败！";
		}
		return msg;
	}

	@Override
	public String orgDisable(HashMap<String, String> params) {
		String msg = null;
		int num = orgDao.orgDisable(params);
		if (num == 0) {
			msg = "组织停用失败！";
		}
		return msg;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Map orgByOrgcode(HashMap<String, String> params) {
		return orgDao.orgByOrgcode(params);
	}

	@Override
	public String orgTreebig(HashMap<String, String> params) {
		return orgDao.orgTreebig(params);
	}

}
